package com.yyx.app1.Data.DiagnosticTools;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yyx.app1.Data.DataInit;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class RemedyInfo0_SQLiteOperation {
    private DataInit dataInit;
    private String TableName;

    public RemedyInfo0_SQLiteOperation(DataInit dataInit) {
        this.dataInit =dataInit;
        TableName = dataInit.getDB_RemedyInfoTableName();
    }

    public void deleteAll()
    {
        SQLiteDatabase db=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            db.execSQL("delete from " + TableName + " ;");
//            db.execSQL("update "+TableName+" SET number = 0 ;");
        }
        catch (Exception e)
        {
          Log.e("异常1509301901", e.toString());
        }
        finally
        {
            Log.i("RemedyInfo", "deleteAll");
            if (db!=null)
                db.close();
        }
    }

    public long add(RemedyInfo remedyInfo) {
        long result=0;
        SQLiteDatabase db=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            ContentValues values = new ContentValues();
            values.put("id",remedyInfo.getId());//private String id;
            values.put("chineseName",remedyInfo.getChineseName());//private String chineseName; // 中文名称
            values.put("curtailment",remedyInfo.getCurtailment());//private String curtailment; // 缩写
            values.put("position",remedyInfo.getPosition());//private String position;  // 产生部位
            values.put("summarize",remedyInfo.getSummarize());//private String summarize;  // 概述
            values.put("highMean",remedyInfo.getHighMean());//private String highMean; // 指标过高意义
            values.put("lowMean",remedyInfo.getLowMean());//private String lowMean; //指标过低意义
            values.put("type",remedyInfo.getType());//private String type; // 工具
            values.put("createTime",remedyInfo.getCreateTime());//private String createTime; //时间
            result = db.insert(TableName,null,values);
        }
        catch (Exception e)
        {
            Log.e("异常1509301745",e.toString());
        }
        finally
        {
            if (db!=null)
                db.close();
            return result;
        }
    }

    public long add(RemedyInfo remedyInfo,SQLiteDatabase db) {
        long result=0;
        try
        {
            ContentValues values = new ContentValues();
            values.put("id",remedyInfo.getId());//private String id;
            values.put("chineseName",remedyInfo.getChineseName());//private String chineseName; // 中文名称
            values.put("curtailment",remedyInfo.getCurtailment());//private String curtailment; // 缩写
            values.put("position",remedyInfo.getPosition());//private String position;  // 产生部位
            values.put("summarize",remedyInfo.getSummarize());//private String summarize;  // 概述
            values.put("highMean",remedyInfo.getHighMean());//private String highMean; // 指标过高意义
            values.put("lowMean",remedyInfo.getLowMean());//private String lowMean; //指标过低意义
            values.put("type",remedyInfo.getType());//private String type; // 工具
            values.put("createTime",remedyInfo.getCreateTime());//private String createTime; //时间
            result = db.insert(TableName, null, values);
        }
        catch (Exception e)
        {
          Log.e("异常1509301744",e.toString());
        }
        finally
        {
            return result;
        }
    }

    public List<HashMap<String,String>> fuzzySearch(String searchname)
    {
        List<HashMap<String,String>> list=new ArrayList<>();
        SQLiteDatabase db=null;
        Cursor cursor=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"distinct chineseName", "curtailment"}, "chineseName like ? or curtailment like ?", new String[]{"%" + searchname + "%", "%" + searchname + "%"}, null, null, null, null);
            while (cursor.moveToNext())
            {
                HashMap<String,String> map = new HashMap<>();
                map.put("chineseName",cursor.getString(cursor.getColumnIndex("chineseName")));
                map.put("curtailment",cursor.getString(cursor.getColumnIndex("curtailment")));
                list.add(map);
            }
        }
        catch (Exception e)
        {
          Log.e("异常1509301743",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return list;
        }
    }

    public boolean find(String chineseName) {
        boolean result=false;
        SQLiteDatabase db=null;
        try
        {
            db = dataInit.creatOrOpenDatabase();
            Cursor cursor = db.query(TableName, null, "chineseName=?", new String[]{chineseName}, null, null, null);
            result = cursor.moveToNext();
            if (cursor!=null)
                cursor.close();
        }
        catch (Exception e)
        {
          Log.e("异常1509301740",e.toString());
        }
        finally
        {
            if (db!=null)
                db.close();
            return result;
        }
    }

    public JSONObject getByChineseName(String chineseName) {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        JSONObject jsonObject=new JSONObject();
        try
        {
            db = dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, null, "chineseName=?", new String[]{chineseName}, null, null, null, null);
            if (cursor.moveToNext())
            {
                jsonObject.put("id",cursor.getString(cursor.getColumnIndex("id")));    //private String id;
                jsonObject.put("chineseName",cursor.getString(cursor.getColumnIndex("chineseName")));    //private String chineseName; // 中文名称
                jsonObject.put("curtailment",cursor.getString(cursor.getColumnIndex("curtailment")));    //private String curtailment; // 缩写
                jsonObject.put("position",cursor.getString(cursor.getColumnIndex("position")));    //private String position;  // 产生部位
                jsonObject.put("summarize",cursor.getString(cursor.getColumnIndex("summarize")));    //private String summarize;  // 概述
                jsonObject.put("highMean",cursor.getString(cursor.getColumnIndex("highMean")));    //private String highMean; // 指标过高意义
                jsonObject.put("lowMean",cursor.getString(cursor.getColumnIndex("lowMean")));    //private String lowMean; //指标过低意义
                jsonObject.put("type",cursor.getString(cursor.getColumnIndex("type")));    //private String type; // 工具
                jsonObject.put("createTime",cursor.getString(cursor.getColumnIndex("createTime")));    //private String createTime; //时间
            }
        }
        catch (Exception e)
        {
          Log.e("异常1509301748",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return jsonObject;
        }
    }

    public List getDemo_10name() {
        SQLiteDatabase db=null;
        Cursor cursor=null;
        List<HashMap<String,String>> list=new ArrayList<>();
        try
        {
            db = dataInit.creatOrOpenDatabase();
            cursor = db.query(TableName, new String[]{"chineseName","curtailment"}, "type = ?", new String[]{"0"},null,null,null);
            int i=0;
            while (cursor.moveToNext())
            {
                if (i>14)
                {
                    break;
                }
                HashMap<String,String> map= new HashMap<>();
                map.put("chineseName",cursor.getString(cursor.getColumnIndex("chineseName")));
                map.put("curtailment",cursor.getString(cursor.getColumnIndex("curtailment")));
                list.add(map);
                ++i;
            }
        }
        catch (Exception e)
        {
          Log.e("异常1509301749",e.toString());
        }
        finally
        {
            if (cursor!=null)
                cursor.close();
            if (db!=null)
                db.close();
            return list;
        }
    }

    public void InjectionSQList()
    {
        SQLiteDatabase db = dataInit.creatOrOpenDatabase();
        File file=new File(dataInit.getDataBaseDirectory()+"/"+dataInit.getRemedyInfoFileName());
        try
        {
            BufferedReader bufferedReader=new BufferedReader(new FileReader(file));
            String strline;
            while ((strline=bufferedReader.readLine())!=null)
            {
                JSONObject jsonObject=new JSONObject(strline);
                RemedyInfo remedyInfo = new RemedyInfo();
                remedyInfo.setId(jsonObject.getString("id"));//private String id;
                remedyInfo.setChineseName(jsonObject.getString("chineseName"));//private String chineseName; // 中文名称
                remedyInfo.setCurtailment(jsonObject.getString("curtailment"));//private String curtailment; // 缩写
                remedyInfo.setPosition(jsonObject.getString("position"));//private String position;  // 产生部位
                remedyInfo.setSummarize(jsonObject.getString("summarize"));//private String summarize;  // 概述
                remedyInfo.setHighMean(jsonObject.getString("highMean"));//private String highMean; // 指标过高意义
                remedyInfo.setLowMean(jsonObject.getString("lowMean"));//private String lowMean; //指标过低意义
                remedyInfo.setType(jsonObject.getString("type"));//private String type; // 工具
                remedyInfo.setCreateTime(jsonObject.getString("createTime"));//private String createTime; //时间
                add(remedyInfo,db);
            }
            bufferedReader.close();
            Log.i("RemedyInfo", "结束");
        }
        catch (Exception e)
        {
          Log.e("异常1509301751", e.toString());
        }
        finally
        {
            if (db!=null)
                db.close();
        }
    }
}
